/**
 * 
 */

var tbodyAvatarListObj = null;

$(window.document).ready(function(){
	tbodyAvatarListObj = $("tbody[id='tbodyAvatarList']");
	
	syncPtInf();
});

function refreshAvatar() {
	var url = "refreshAvatar.json";
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data: null,
		async: true,
		success: function(data){
			avatarVOList = data.avatarVOList;
			if( avatarVOList == null ) {
				alert("refreshAvatar() - fail:data is null");
				return null;
			}

			tbodyAvatarListObj.children().remove();
			var i, avatarVO;
			var html = "";
			for(i=0; i<avatarVOList.length; i++) {
				avatarVO = avatarVOList[i];
				html += "<tr id='trAvatarInfo_" + avatarVO.avatarId + "'>"
					+ "<td><span>" + (i+1) + "</span></td>"
					+ "<td><span>" + avatarVO.avatarId + "</span></td>"
					+ "<td><span>" + avatarVO.posX + "</span></td>"
					+ "<td><span>" + avatarVO.posY + "</span></td>"
					+ "<td><span>" + avatarVO.avatarScore + "</span></td>"
					+ "<td><input type=\"number\" id=\"avatarScore_" + avatarVO.avatarId + "\" maxlength=\"3\" style=\"width:50px;\" /><input type=\"button\" id=\"buttonDeleteAvatar_" + avatarVO.avatarId + "\" value=\"SET\" onclick=\"setAvatarScore('" + avatarVO.avatarId + "');\" /></td>"
					+ "<td><input type=\"button\" id=\"buttonDeleteAvatar_" + avatarVO.avatarId + "\" value=\"DELETE\" onclick=\"deleteAvatar('" + avatarVO.avatarId + "');\" /></td>"
					+ "</tr>";
			}
			$("#userCount").text(avatarVOList.length);
			tbodyAvatarListObj.append(html);
			syncPtInf(data);
		},
		error: function(){
			alert("refreshAvatar() - fail:unknown");
		}
	});
}

function deleteAvatar(avatarId) {
	if( confirm(avatarId + " - 삭제하시겠습니까?") ) {
		var url = "deleteAvatar.json";
		$.ajax({
			type: "POST",
			url: url,
			dataType: "json",
			data: "avatarId="+avatarId,
			async: true,
			success: function(data){
				$("#trAvatarInfo_" + avatarId).remove();
				syncPtInf(data);
			},
			error: function(){
				alert("deleteAvatar(avatarId) - fail:unknown");
			}
		});
	}
}

function toggleOpts(type,obj){
	if( type == "OBJ" ) {
		$(obj).parent().parent().find("#oxOptsDiv").hide();
		$(obj).parent().parent().find("#optsDiv").show();
	}
	else if( type == "SUB" ) {
		$(obj).parent().parent().find("#optsDiv").hide();
		$(obj).parent().parent().find("#oxOptsDiv").hide();
	}
	else if( type == "OX" ) {
		$(obj).parent().parent().find("#optsDiv").hide();
		$(obj).parent().parent().find("#oxOptsDiv").show();
	}
}

//설문 시작
function startPoll(obj) {
	var url = "startPoll.json";
	var qstTypeCd = $(obj).parent().parent().find("input:radio[name^='pollObjSubFlag']:checked").val();
	if( qstTypeCd==null || qstTypeCd.length==0 ) {
		alert("문항 종류를 선택하세요.");
		$(obj).parent().parent().find("input:radio[name='pollObjSubFlag']").focus();
		return null;
	}
	var qstVal = $(obj).parent().parent().find("#qstVal").val();
	if( qstVal==null || qstVal.length==0 ) {
		alert("문제를 입력하세요.");
		$(obj).parent().parent().find("#qstVal").focus();
		return null;
	}
	var param = "qstTypeCd="+qstTypeCd+"&qstVal="+qstVal;
	if( qstTypeCd == "OBJ" ) {
		var objOpt1 = $(obj).parent().parent().find("#objOpt1").val();
		var objOpt2 = $(obj).parent().parent().find("#objOpt2").val();
		var objOpt3 = $(obj).parent().parent().find("#objOpt3").val();
		var objOpt4 = $(obj).parent().parent().find("#objOpt4").val();
		var optPoint1 = $(obj).parent().parent().find("#optPoint1").val();
		var optPoint2 = $(obj).parent().parent().find("#optPoint2").val();
		var optPoint3 = $(obj).parent().parent().find("#optPoint3").val();
		var optPoint4 = $(obj).parent().parent().find("#optPoint4").val();
		param += ("&objOpt1="+objOpt1+"&objOpt2="+objOpt2+"&objOpt3="+objOpt3+"&objOpt4="+objOpt4
				+"&optPoint1="+optPoint1+"&optPoint2="+optPoint2+"&optPoint3="+optPoint3+"&optPoint4="+optPoint4);
	}
	else if( qstTypeCd == "OX" ) {
		var objOpt1 = $(obj).parent().parent().find("#oxOpt1").val();
		var objOpt2 = $(obj).parent().parent().find("#oxOpt2").val();
		var optPoint1 = $(obj).parent().parent().find("#oxOptPoint1").val();
		var optPoint2 = $(obj).parent().parent().find("#oxOptPoint2").val();
		param += ("&objOpt1="+objOpt1+"&objOpt2="+objOpt2
				+"&optPoint1="+optPoint1+"&optPoint2="+optPoint2);
	}

	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data: param,
		async: true,
		success: function(data){
			if( data == null ) {
				alert("startPoll() - fail:data is null");
				return null;
			}
			//현재 상태 반영
			$("#inputPtId").val(data.ptInfVO.ptId);
			$("#inputPstPollId").val(data.ptInfVO.pstPollId);
			$("#selectPtStatCd").val(data.ptInfVO.ptStatCd);
			$(obj).parent().find("#ptId").val(data.ptId);
			$(obj).parent().find("#pollId").val(data.pollId);
			$(obj).parent().find("#buttonStartPoll").hide();
			$(obj).parent().find("#loadingImg").show();
			$(obj).parent().find("#buttonTerminatePoll").show();
			syncPtInf(data);
			alert("startPoll() - success");
		},
		error: function(){
			alert("startPoll() - fail:unknown");
		}
	});
}

function terminatePoll(obj) {
	var url = "terminatePoll.json";
	var ptId = $(obj).parent().find("#ptId").val();
	var pollId = $(obj).parent().find("#pollId").val();
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data: "ptId="+ptId+"&pollId="+pollId,
		async: true,
		success: function(data){
			if( data == null ) {
				alert("terminatePoll() - fail:data is null");
				return null;
			}
			
			$("#inputPtId").val(data.ptInfVO.ptId);
			$("#selectPtStatCd").val(data.ptInfVO.ptStatCd);
			$("#inputPstPollId").val(data.ptInfVO.pstPollId);
			
			$(obj).parent().find("#buttonStartPoll").show();
			$(obj).parent().find("#loadingImg").hide();
			$(obj).parent().find("#buttonTerminatePoll").hide();
			
//			$("#qVal").attr("readonly", false);
//			$("#objAsw1").attr("readonly", false);
//			$("#objAsw2").attr("readonly", false);
//			$("#objAsw3").attr("readonly", false);
//			$("#objAsw4").attr("readonly", false);
//			$("#inputPtId").val("");
//			$("#spanPollId").text("");
//			$("#spanPollStatus").hide();
			syncPtInf(data);
			alert("terminatePoll() - success");
		},
		error: function(){
			alert("terminatePoll() - fail:unknown");
		}
	});
}

function goStat(gubun, val){
	var url = "alterPtStatCd.json";
	var ptId = $("#inputPtId").val();
	var ptStatCd;
	
	if(gubun == 'sel'){
		ptStatCd= $("#selectPtStatCd").val();
	}
	else{
		ptStatCd=val;
	}
	
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data: "ptId="+ptId+"&ptStatCd="+ptStatCd,
		async: true,
		success: function(data){
			if( data == null ) {
				alert("goStat() - fail:data is null");
				return null;
			}
			syncPtInf(data);
		},
		error: function(){
			alert("goStat() - fail:unknown");
		}
	});
}

function startSequencialPractice() {
	
	var url = "startSequencialPractice.json";
	var jsonMessages = [];
	
	var trs = $("#practiceData tr");
	var message,sustainmentTime;
	$(trs).each(function() {
		message = $("textarea[name='practiceMessage']", this).val();
		sustainmentTime = $("input[name='sustainmentTime']", this).val();
		jsonMessages[jsonMessages.length] = {"message":message, "sustainmentTime":sustainmentTime};
	});
	
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data: "practiceData="+JSON.stringify(jsonMessages),
		async: true,
		success: function(data){
		},
		error: function(){
			alert("practice() - fail:unknown");
		}
	});
}

function setAvatarScore(avatarId) {
	var avatarScore = $("#avatarScore_"+avatarId).val();
	if( avatarScore==null || avatarScore.length==0 ) return null;
	if( !confirm(avatarId + " - " + avatarScore + "\n바꿀거야?") ) return null;

	var url = "setAvatarScore.json";
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data: "avatarId="+avatarId+"&avatarScore="+avatarScore,
		async: true,
		success: 	function(data){ refreshAvatar(); },
		error: 		function(){ }
	});
}


function sendMessage(obj){
	
	debugger;
	if(obj){
		var practiceMessage = $(obj).parent().parent().find("#practiceMessage").val();
		var url = "sendMessage.json";
		
		$.ajax({
			type: "POST",
			url: url,
			dataType: "json",
			data: "practiceMessage="+practiceMessage,
			async: true,
			success: function(data){
				alert("메시지 전송 완료 : "+practiceMessage);
			},
			error: function(){
				alert("practice() - fail:unknown");
			}
		});
	}
}

function terminatePractice(){
	if(obj){
		var url = "terminateMessage.json";
		
		$.ajax({
			type: "POST",
			url: url,
			dataType: "json",
			async: true,
			success: function(data){
			},
			error: function(){
				alert("practice() - fail:unknown");
			}
		});
	}	
}

function syncPtInf(data){
	if(data && data.ptInfVO){
		$("#inputPtId").val(data.ptInfVO.ptId);
		$("#inputPtName").val(data.ptInfVO.ptName);
		$("#inputPstPollId").val(data.ptInfVO.pstPollId);
		$("#ptStatCdHidden").val(data.ptInfVO.ptStatCd);
	}
	$("#ptStatNameSpan").text(getPtStatNm($("#ptStatCdHidden").val()));
}